--[[
key1 - orderKey
key2 - sumKey
key3 - zsetKey

arg1 - orderValue
arg2 - quantity
arg3 - buyerId
arg4 - timestamp
--]]

-- 幂等性key-value
if redis.call("EXISTS",KEYS[1]) == 1 then
  return "idempotency violation"
end
redis.call('set',KEYS[1],ARGV[1])
redis.call('expire',KEYS[1],1800)

-- 专辑总量
redis.call('incrby', KEYS[2], ARGV[2])
redis.call('expire',KEYS[2],60*60*24*5)

-- 获取旧score
local oldScore = redis.call('zscore', KEYS[3],ARGV[3])
local oldQuantity = 0
if(oldScore)
then
	-- 解析购买量
	oldQuantity = string.sub(oldScore, 1 ,string.len(oldScore) - 10)
end
-- 组装新score
local newScore = (oldQuantity + ARGV[2]) .. ARGV[4]
-- zadd newScore
redis.call('zadd',KEYS[3], newScore, ARGV[3])
redis.call('expire',KEYS[3], 60*60*24*5)

return "ok"